package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.Constant;
import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.SchoolIcCard;

public class SchoolIcCardRowMapper implements RowMapper<SchoolIcCard> {

    private int type;
    public static final int AND_CARDNUM=1;
    public static final int AND_CARDNUM_NORMAL=2;
    public static final int AND_CARDNUM_STUDENT=3;
    
    public static final int AND_SNAME=4;
    public static final int AND_CNAME = 5;
    public static final int AND_NEW = 6;
    
    public SchoolIcCardRowMapper() {
        
    }

    public SchoolIcCardRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SchoolIcCard mapRow(ResultSet rs, int rowNum) throws SQLException {
        SchoolIcCard sc = parseSchoolIcCard(new SchoolIcCard(), rs);
        sc.setBsdate_str(SystemDateFormatUtil.formatDate(sc.getBsdate(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
        if(sc.getState()==Constant.num_10){
            sc.setState_str("启用");
        }else{
            sc.setState_str("注销");
        }
        
        switch (type) {
            case AND_CARDNUM:
                sc.setCardnum(rs.getString("cardnum"));
                String name_student = rs.getString("name_student");
                String name_teacher = rs.getString("name_teacher");
                sc.setName_student(name_student);
                sc.setName_teacher(name_teacher);
                if(name_student!=null){
                    sc.setName_card("学生:"+name_student);
                }else if(name_teacher!=null){
                    sc.setName_card("教师:"+name_teacher);
                }else{
                    sc.setName_card("未分派");
                }
                break;
            case AND_CARDNUM_NORMAL:
                sc.setCardnum(rs.getString("cardnum"));
                break;
            case AND_CARDNUM_STUDENT:
                sc.setCardnum(rs.getString("cardnum"));
                sc.setIfoot(rs.getString("icfoot"));
                sc.setName_student(rs.getString("name_student"));
                sc.setSname(rs.getString("sname"));
                sc.setCname(rs.getString("cname"));
                break;
            case AND_SNAME:
                sc.setCardnum(rs.getString("cardnum"));
                String name_student1 = rs.getString("name_student");
                String name_teacher1 = rs.getString("name_teacher");
                sc.setName_student(name_student1);
                sc.setName_teacher(name_teacher1);
                if(name_student1!=null){
                    sc.setName_card("学生:"+name_student1);
                }else if(name_teacher1!=null){
                    sc.setName_card("教师:"+name_teacher1);
                }else{
                    sc.setName_card("未分派");
                }
                sc.setSname(rs.getString("sname"));
                break;
            case AND_CNAME:
                sc.setCardnum(rs.getString("cardnum"));
                sc.setName_student(rs.getString("name_student"));
                sc.setCname(rs.getString("cname"));
                if(sc.getState()==Constant.num_10){
                    sc.setState_str("已分派");
                }else{
                    sc.setState_str("已挂失");
                }
                break;
            case AND_NEW:
                sc.setCardnum(rs.getString("cardnum"));
                String sname = rs.getString("name_student");
                String tname = rs.getString("name_teacher");
                if(sname!=null){
                    sc.setName_card(sname);
                    sc.setIdentity("学生");
                    sc.setState_str("已分派");
                }else if(tname!=null){
                    sc.setName_card(tname);
                    sc.setIdentity("教工");
                    sc.setState_str("已分派");
                }else{
                    sc.setName_card("无");
                    sc.setIdentity("无");
                    sc.setState_str("未分派");
                }
                if (null != rs.getString("cname")) {
                    sc.setCname(rs.getString("cname"));
                } else {
                    sc.setCname("无");
                }
                if(sc.getState()==Constant.num_20){
                    sc.setState_str("已挂失");
                }
                
                break;
        }
        return sc;
    }

    public static SchoolIcCard parseSchoolIcCard(SchoolIcCard s, ResultSet rs) throws SQLException {
        return parseSchoolIcCard(s, rs, null);
    }

    public static SchoolIcCard parseSchoolIcCard(SchoolIcCard s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setSiid(rs.getLong(su+"siid"));
        } catch (SQLException e) {
            s.setSiid(null);
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setIcid(rs.getLong(su+"icid"));
        } catch (SQLException e) {
            s.setIcid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setBsdate(rs.getTimestamp(su+"bsdate"));
        } catch (SQLException e) {
            s.setBsdate(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SchoolIcCard s) {
        par.put("siid", s.getSiid());
        par.put("sid", s.getSid());
        par.put("icid",s.getIcid());
        par.put("cid",s.getCid());
        par.put("state", s.getState());
        par.put("bsdate",s.getBsdate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SchoolIcCard s) {
        args.put("siid",s.getSiid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SchoolIcCard s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "siid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(SchoolIcCard s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school_icard(");
        int count = 0;
        if (null != s.getSid()) {
            sql.append("sid,");
            count++;
        }
        
        if (null != s.getIcid()) {
            sql.append("icid,");
            count++;
        }

        if (null !=s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null !=s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != s.getBsdate()) {
            sql.append("bsdate,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SchoolIcCard s)
        throws SQLException {
        int count = 1;
        if (null != s.getSid()) {
            ps.setLong(count++,s.getSid());
        }
        if (null != s.getIcid()) {
            ps.setLong(count++, s.getIcid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++, s.getCid());
        }
        if (null != s.getState()) {
            ps.setShort(count++,s.getState());
        }
        if (null != s.getBsdate()) {
            ps.setTimestamp(count++,s.getBsdate());
        }


    }

}
